package com.hh.base.widget.dialog

import android.app.Activity
import android.content.Context
import android.view.View

/**
 * Create By hHui on 2025/4/8 15:52
 *
 * @description
 */
class MultifunctionDialog {
    private val context: Context
    private var iosAlertDialog: IosAlertDialog? = null
    private var loadingDialog: LoadingDialog? = null


    constructor(context: Context) {
        this.context = context
    }

    /**
     * 显示loading
     */
    fun showLoading(msg: String, canCancel: Boolean, cancelText: String, callback: AlertCancelListener?) {
        closeAllDialog()
        if (loadingDialog == null) {
            loadingDialog = LoadingDialog()
            loadingDialog?.init(context)
        }
        if (!canCancel) {
            loadingDialog?.showLoading(msg)
        } else {
            loadingDialog?.showLoadingCanCancel(msg, cancelText, callback)
        }
    }

    /**
     * 关闭loading
     */
    fun closeLoading() {
        if (context is Activity) {
            if (!context.isFinishing && context.isDestroyed) {
                loadingDialog?.closeProgress()
            }
        } else {
            loadingDialog?.closeProgress()
        }
    }

    /**
     * 只显示消息
     */
    fun alertMessage(msg: String) {
        closeAllDialog()
        iosAlertDialog = IosAlertDialog(context).builder().setMsg(msg)
        iosAlertDialog?.setCanDismiss(true)
        iosAlertDialog?.setCancelable(false)
        iosAlertDialog?.show()
    }

    /**
     * 显示标题和内容
     */
    fun alertTitleAndMessage(title: String, msg: String) {
        iosAlertDialog = IosAlertDialog(context).builder().setTitle(title).setMsg(msg)
        iosAlertDialog?.setCanDismiss(true)
        iosAlertDialog?.setCancelable(false)
        iosAlertDialog?.show();
    }

    /**
     * 带取消和确定的弹窗
     */
    fun alertOption(title: String, msg: String, callback: AlertClickListener?, sureText: String, cancelText: String) {
        iosAlertDialog = IosAlertDialog(context).builder().setMsg(msg).setTitle(title).setPositiveButton(sureText) {
            iosAlertDialog?.dismiss()
            callback?.onConfirm()
        }.setNegativeButton(cancelText) {
            iosAlertDialog?.dismiss()
            callback?.onCancel()
        }
        iosAlertDialog?.show()
    }


    private fun closeAllDialog() {
        if (loadingDialog != null && loadingDialog!!.isShowing) {
            loadingDialog?.closeProgress()
        }

        if (iosAlertDialog != null && iosAlertDialog!!.isShowing) {
            iosAlertDialog?.dismiss()
        }
    }


    interface AlertClickListener : AlertCancelListener {
        fun onConfirm(content: String? = null)
    }

    interface AlertCancelListener {
        fun onCancel()
    }

}
